package com.ypd.shop.service.impl;

import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import com.ypd.common.core.domain.entity.SysUser;
import com.ypd.common.utils.SecurityUtils;
import com.ypd.shop.domain.vo.CommentVo;
import com.ypd.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ypd.shop.mapper.CommentMapper;
import com.ypd.shop.domain.Comment;
import com.ypd.shop.service.ICommentService;

/**
 * 文章评论Service业务层处理
 * 
 * @author lin
 * @date 2025-01-16
 */
@Service
public class CommentServiceImpl implements ICommentService 
{
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询文章评论
     * 
     * @param id 文章评论主键
     * @return 文章评论
     */
    @Override
    public Comment selectCommentById(Long id)
    {
        return commentMapper.selectCommentById(id);
    }


    /**
     * 查询文章评论列表
     * 
     * @param comment 文章评论
     * @return 文章评论
     */
    @Override
    public List<CommentVo> selectCommentList(Comment comment)
    {
        //根据userId获取用户信息并进行注入
        List<Comment> comments = commentMapper.selectCommentList(comment);
        ArrayList<CommentVo> commentVos = new ArrayList<>();
        for (int i = 0; i < comments.size(); i++) {
            CommentVo vo = new CommentVo();
            BeanUtil.copyProperties(comments.get(i),vo);
            SysUser user = sysUserService.selectUserById(comments.get(i).getUserId());
            if (user.getAvatar() != null)vo.setAvatar(user.getAvatar());
            if (user.getNickName() != null)vo.setAuthor(user.getNickName());
            commentVos.add(vo);
        }
        return commentVos;
    }

    /**
     * 新增文章评论
     * 
     * @param comment 文章评论
     * @return 结果
     */
    @Override
    public int insertComment(Comment comment)
    {
        //获取用户id
        comment.setUserId(SecurityUtils.getUserId());
        return commentMapper.insertComment(comment);
    }

    /**
     * 修改文章评论
     * 
     * @param comment 文章评论
     * @return 结果
     */
    @Override
    public int updateComment(Comment comment)
    {
        return commentMapper.updateComment(comment);
    }

    /**
     * 批量删除文章评论
     * 
     * @param ids 需要删除的文章评论主键
     * @return 结果
     */
    @Override
    public int deleteCommentByIds(Long[] ids)
    {
        return commentMapper.deleteCommentByIds(ids);
    }

    /**
     * 删除文章评论信息
     * 
     * @param id 文章评论主键
     * @return 结果
     */
    @Override
    public int deleteCommentById(Long id)
    {
        return commentMapper.deleteCommentById(id);
    }
}
